<?php

define('MULTIPLE_SHOW', 1);
define('FAST_SHOW', 2);

class Xzsdsplus_Costpriceplus_Block_Show extends Mage_Adminhtml_Block_Widget_Container {
	public function __construct() {
		parent::__construct();
		$this->setTemplate('costpriceplus/banner.phtml');
	}
	
	public function getBannerList() {

		$list = '';
		$baseurl = Mage::getBaseUrl('media');
		$category_id = $this->getCurrentCategory()->getEntity_id();
		$db = Mage::getSingleton('core/resource')->getConnection('core_read');
		$result = $db->query("
			SELECT b.title, b.linkto, b.image FROM costpriceplus_category c LEFT JOIN costpriceplus b 
			ON b.banner_id = c.banner_id  
			WHERE  b.status = 1 AND (b.end_at > NOW() OR b.end_at IS NULL) AND (c.category_id=1 OR c.category_id={$category_id})");
		
		while($banner = $result->fetch()) {
			$img = $baseurl. $banner['image'];
			if (!empty($img)){
				$url = $banner['linkto'];
				if (empty($url)) {
					$url = '#';
				}else if (strpos($url, 'http://') === FALSE){
					$url='http://'. $url;
				}
				$list .= '<li><a href="'. $img. '" title="'. $banner['title']. '" ></a><a href="'. $url. '" target="_blank"></a></li>';
			}
		}
		
		if (empty($list)) {
			$list = '<li><a href="'. $this->getSkinUrl('images/banner.jpg').'" title=""></a><a href="#" target="_blank"></a> </li>';
		}
		
		return $list;
	}
	public function getCurrentCategory() {
		if (Mage::getSingleton('catalog/layer')) {
			return Mage::getSingleton('catalog/layer')->getCurrentCategory();
		}
		return false;
	}
	
	protected function _prepareLayout() {
		return parent::_prepareLayout();
	}
	
	public function getStoreCategories() {
		$helper = Mage::helper('catalog/category');
		return $helper->getStoreCategories();
	}
	
}

